<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

/**
 * 轮播图控制器类
 * Class ScrolledController
 * @package App\Http\Controllers\Admin
 */
class ScrolledController extends Controller
{
    /**
     * 展示轮播图管理页
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $num = $request->input('num', 10);

        $data = DB::table('scrolled_img')
            ->where(function ($query) use ($request) {
                $query->where('status', 'like', '%' . $request->input('keyword') . '%')->where('created_at', '!=', '');
            })
            ->paginate($num);

        return view(('admin.scrolled.index'), ['data' => $data, 'request' => $request->all()]);
    }

    /**
     * 显示添加轮播图页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {
        return view('admin.scrolled.add');
    }

    /**
     * 执行轮播图插入操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function insert(Request $request)
    {
        $data = $request->except('_token');
        $data['created_at'] = date("Y-m-d H:i:s", time());
        $data['updated_at'] = '1970-01-01 08:00:00';

        $image = $request->file('image'); //使用laravel 自带的request类来获取一下文件.
        $filedir = "upload/scrolled_images/"; //定义图片上传路径
        $suffix = $image->getClientOriginalExtension(); //获取上传图片的文件名
        $newImagesName = md5(time()) . mt_rand(100000, 999999) . "." . $suffix; //重新命名上传文件名字
        $image->move($filedir, $newImagesName); //使用move 方法移动文件.

        $data['image'] = $newImagesName;

        $res = DB::table('scrolled_img')->insert($data);

        if ($res) {
            return redirect('admin/scrolled/index')->with(['info' => '添加成功']);
        } else {
            return back()->with(['info' => '添加失败']);
        }
    }

    /**
     * 显示编辑页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit()
    {
        return view('admin.scrolled.edit');
    }

    /**
     * 执行更新操作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request)
    {
        $data = $request->except('_token');

        $id = $request->input('id');
        $oldImage = DB::table('scrolled_img')->where('id', '=', $id)->first()->image;
        if ($request->hasFile('image') && $request->file('image')->isValid()) {
            $data = $request->except('_token');

            $image = $request->file('image'); //使用laravel 自带的request类来获取一下文件.
            $filedir = "upload/scrolled_images/"; //定义图片上传路径
            $suffix = $image->getClientOriginalExtension(); //获取上传图片的文件名
            $newImagesName = md5(time()) . mt_rand(100000, 999999) . "." . $suffix; //重新命名上传文件名字
            $image->move($filedir, $newImagesName); //使用move 方法移动文件.

            $data['image'] = $newImagesName;
            $data['updated_at'] = date("Y-m-d H:i:s", time());

            if (file_exists('upload/scrolled_images/'.$oldImage)) {
                unlink('upload/scrolled_images/'.$oldImage);
            }

        }

        $res = DB::table('scrolled_img')->where('id', '=', $id)->update($data);
        if ($res) {
            return redirect('admin/scrolled/index')->with(['info' => '更新成功']);
        } else {
            return back()->with(['info' => '更新失败']);
        }

    }

    /**
     * 执行轮播图删除操作
     * @param $id
     * @return false|\Illuminate\Http\RedirectResponse|string
     */
    public function delete($id)
    {
        $updated_at = date("Y-m-d H:i:s", time());
        $res = DB::table('scrolled_img')->where('id', '=', $id)->update(['created_at' => '', 'updated_at' => $updated_at]);

        if ($res) {
            return redirect('admin/scrolled/index')->with(['info' => '删除成功']);
        } else {
            return redirect('admin/scrolled/index')->with(['info' => '删除失败']);
        }


        return date("Y-m-d H:i:s", time());
    }
}
